package com.masterclient.app.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.masterclient.app.R;
import com.masterclient.app.modules.bridge.TaskRetainBean;
import com.masterclient.app.modules.goods.adapter.TaskRetainRecyclerAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auther hxm
 * @date 2019/12/17.
 */
public class TaskRetainDialog extends Dialog {

    @BindView(R.id.tv_des)
    TextView tvDes;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    @BindView(R.id.tv_leave)
    TextView tvLeave;
    @BindView(R.id.tv_complete)
    TextView tvComplete;
    private Context context;
    private TaskRetainRecyclerAdapter taskRetainRecyclerAdapter;

    public TaskRetainDialog(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    public void setData(TaskRetainBean taskRetainBean,OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;

        tvDes.setText(taskRetainBean.data.noticeTopText);
        taskRetainRecyclerAdapter.setNewData(taskRetainBean.data.rewardItems);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_task_retain);

        ButterKnife.bind(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setItemAnimator(null);
        taskRetainRecyclerAdapter = new TaskRetainRecyclerAdapter();
        recyclerView.setAdapter(taskRetainRecyclerAdapter);
        taskRetainRecyclerAdapter.bindToRecyclerView(recyclerView);

        setCanceledOnTouchOutside(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.TOP);
        window.setBackgroundDrawableResource(R.color.transparent);
        window.setAttributes(windowParams);
    }

    private OnChooseListener onChooseListener;
    public void setOnChooseListener(OnChooseListener onChooseListener){
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void cancel();

        void confirm();
    }

    @OnClick({R.id.iv_close,R.id.tv_leave,R.id.tv_complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_leave:
                dismiss();
                if (onChooseListener != null) {
                    onChooseListener.cancel();
                }
                break;
            case R.id.iv_close:
            case R.id.tv_complete:
                dismiss();
                if (onChooseListener != null) {
                    onChooseListener.confirm();
                }
                break;
        }
    }
}
